package com.t4l.client.screens.login.signup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.t4l.client.language.Lang;
import com.t4l.client.services.login.signup.SignupService;
import com.t4l.client.services.login.signup.SignupServiceAsync;

public class Signup extends Composite {

	private static SignupUiBinder uiBinder = GWT.create(SignupUiBinder.class);

	interface SignupUiBinder extends UiBinder<Widget, Signup> {
	}

	SignupServiceAsync signupService = GWT.create(SignupService.class);

	@UiField
	Label nameLabel;

	@UiField
	TextBox name;

	@UiField
	Label lastNameLabel;

	@UiField
	TextBox lastName;

	@UiField
	Label emailLabel;

	@UiField
	TextBox email;

	@UiField
	Label passwordLabel;

	@UiField
	PasswordTextBox password;

	@UiField
	Button closeButton;

	@UiField
	Button signupButton;

	DecoratedPopupPanel popup;

	public Signup(final DialogBox dialogBox) {
		initWidget(uiBinder.createAndBindUi(this));
		popup = new DecoratedPopupPanel(true);
		popup.setWidget(new HTML(Lang.get().mandatoryField()));
		closeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});

		signupButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				signup(dialogBox);
			}
		});

	}

	protected void signup(final DialogBox dialogBox) {
		String _name = name.getText();
		String _lastName = lastName.getText();
		String _email = email.getText();
		String _password = password.getText();
		if (_name.trim().length() == 0) {
			showWarning(name.getElement().getAbsoluteLeft() + 270, name
					.getElement().getAbsoluteTop());
			name.setFocus(true);
		} else if (_lastName.trim().length() == 0) {
			showWarning(lastName.getElement().getAbsoluteLeft() + 270, lastName
					.getElement().getAbsoluteTop());
			lastName.setFocus(true);
		} else if (_email.trim().length() == 0) {
			showWarning(email.getElement().getAbsoluteLeft() + 270, email
					.getElement().getAbsoluteTop());
			email.setFocus(true);
		} else if (_password.trim().length() == 0) {
			showWarning(password.getElement().getAbsoluteLeft() + 270, password
					.getElement().getAbsoluteTop());
			password.setFocus(true);
		} else {
			signupService.signup(_name, _lastName, _email, _password,
					new AsyncCallback<String>() {
						@Override
						public void onFailure(Throwable caught) {
							Window.alert(caught.getMessage());
						}

						@Override
						public void onSuccess(String result) {
							Window.alert(result);
							dialogBox.hide();
						}
					});
		}
	}

	@Override
	protected void onLoad() {
		super.onLoad();
		name.setFocus(true);
	}

	private void showWarning(int absoluteLeft, int height) {
		popup.setPopupPosition(absoluteLeft, height);
		popup.show();
		Timer timer = new Timer() {
			@Override
			public void run() {
				if (popup.isShowing()) {
					popup.hide();
				}
			}
		};
		timer.schedule(5000);
	}
}
